package Domain.forumManager;

import java.util.Iterator;
import java.util.Vector;
import DB.DataBase;
import objects.User;
import objects.User.Type;

public class UserManager
{
	DataBase _database;
	
	public UserManager(DataBase database)
	{
		this._database = database;
	}
	
	/**
	 * addUser u to the database
	 * @param u the user to be added
	 */
	public void addUser (User u)
	{
		this._database.addUser(u);
	}
	
	/**
	 *  crate User , will crate new User with the name has given 
	 * @param nameOfUser the new name of the user to be added to the database
	 * @return 1 if success -1 if user exist
	 */
	public int createUser(String nameOfUser)
	{
		if(getUserByName(nameOfUser)!=null)
			return -1;
		this._database.addUser(nameOfUser,Type.DefaultUser);
		return 1;
	}
	
	
	/**
	 * remove User by his/her name
	 * @param nameOfUser the name of the user
	 * #return 1 if the user removed 0 otherwish
	 */
	public int removeUser(String nameOfUser)
	{
		User u = this._database.findUser(nameOfUser);
		if (u == null)
			return 0; 
	
	return removeUser(u);
		
	}
	
	
	/**
	 * remove user from the database
	 * @param userToBeRemove the user to be removed 
	 */
	public int removeUser(User userToBeRemove)
	{
		
		try{
			this._database.removeUser(userToBeRemove);
		}
		catch (Exception e)
		{
			return 0;
		}
		return 1;
	}
	
	/**
	 * for tests return the user by is name
	 * @param nameOfUser
	 * @return
	 */
	
	public User getUserByName(String nameOfUser)
	{
		return this._database.findUser(nameOfUser);
	}
	
	
	

	/**
	 * 
	 * @param nameOfUser
	 * @param type 1 for manager , 0 to default user
	 * @return -1 if user doesn't found 1 otherwise
	 */
	public int changeUserType(String nameOfUser,int type)
	{
		User u = _database.findUser(nameOfUser);
		if (u == null)
			return -1;
		if(type==1)
			u.setType(Type.MANAGER);
			_database.updateObject(u);
			
		if(type==0)
			u.setType(Type.DefaultUser);
			_database.updateObject(u);
		return 1;
	}
	/**
	 * 
	 * @param nameOfUser
	 * @return  -1 if user not found -2 if problem unknown
	 */
	public int getTypeOfUser(String nameOfUser)
	{
		User u=_database.findUser(nameOfUser);
		if (u==null)
			return -1;
		else if (u.getType()== User.Type.MANAGER)
				return 1;
		else if (u.getType()== User.Type.DefaultUser)
			return 0;
		else return -2;
	}
	
	
	
	/**
	 
	 * @param nameOfUser
	 * @param userToBeAdded
	 * @return -1 if one of the users doesn't found , 1 if friend added
	 */
	  
	public int addFreind(String nameOfUser,String friendToBeAdded)
	{
		try{
			User user =  _database.findUser(nameOfUser);
			User friend = _database.findUser(friendToBeAdded);
			_database.addFriend(user, friend);
		}
		catch (Exception e)
		{
				return -1;
		}
			return 1;
		
		// the user is register to the freindToBeAdded list
	}
	
	
	
	/**
	 * 
	 * @param nameOfUser
	 * @param friendToBeRemoved
	 * @return @return 1 if friend removed ,-1 if user not found , -2 if userToBeRemoved is not 
	 * friend of nameOfUser
	 */
	public int removeFriend (String nameOfUser,String friendToBeRemoved) 
	{
		
		try{
			 _database.removeFreind(nameOfUser,friendToBeRemoved);
			 return 1;
		}
		catch (NullPointerException e)
		{
				return -1;
		}
			// the user is register to the freindToBeAdded list
			
	}
	
	
	/**
	 * 
	 * @param nameOfUser
	 * @return all the freinds of the user
	 */
	public Vector <User> getFriendsOfUser(String nameOfUser)
	{
		return this._database.getFriendsOfUser(nameOfUser);
	}
	
	public Vector<User> getAllUsers()
	{
		return this._database.getUsers();
	}
	
	
	public Vector<String> getAllUsersAsString()
	{
		Vector<String> ans = new Vector<String>();
		Vector<User> users = this._database.getUsers();
		for (Iterator iterator = users.iterator(); iterator.hasNext();)
		{
			User user = (User) iterator.next();
			ans.add(user.getUserName());
			
		}
		return ans;
	}
	
}
